
/*
File: eqemuJSM.java
Copyright (C) 2011: Ronny L. Bull (aka: Cubber)
Date: 09-18-2011
Development OS: Gentoo Linux
Java Version: sun-jdk-1.6.0.26
Description: EQEmu Java Server Manager

License:

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY except by those people which sell it, which
are required to give you total support for your newly bought product;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/
package eqemujsm.gui;

//Imports
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;
import eqemujsm.console.Console;
import eqemujsm.output.Output;
import eqemujsm.telnet.eqemuTelnet;

public class eqemuJSM extends JFrame implements ActionListener
{

	/**
	 * 
	 */
		private static final long serialVersionUID = 1274462205286625434L;
		
		//Instance Variables
        public static final int WIDTH = 700;
        public static final int HEIGHT = 350;
		private eqemuTelnet telnet;
		//Change default values to empty for production use
		private JTextField svrIp = new JTextField("localhost");
		private JTextField svrPort = new JTextField("9000");
		private JTextField dbName = new JTextField("eq");
		//End change
		private JTextField userName = new JTextField(10);
		private JPasswordField pass = new JPasswordField(10);
		private String server;
		private int port;
		private String db = "none";
		private String user;
		private String pw;
		private String msg;
		private String character;
		private String zone;
		private String account;
		private String newpass;
		private String status;
		private String emoteTarget;
		private String emoteType;
		private JLabel dbStatusLbl = new JLabel("DB Connection: " + db, JLabel.RIGHT);
		private String telnetStatus = "Disconnected";
		private JLabel telnetStatusLbl = new JLabel("Telnet Status: " + telnetStatus, JLabel.RIGHT);
		private String jdbcURL;
		private Connection mysqlConn;
		private JFrame frame;
		private String version = "EQEmuJSM By: Ronny L. Bull - Ver. 0.1.0";
		
		//Main
        public static void main(String[] args)
        {
                //Create the gui object from the eqemuJSM class
                eqemuJSM gui = new eqemuJSM();			    				         

                //Set the gui to visible
                gui.setVisible(true);
        }

        //GUI
        public eqemuJSM()
        {
                //Setup Window
                //Window Title
                super("The EverQuest Emulator Java Server Manager");

                //Window Size
                setSize(WIDTH, HEIGHT);

                //Close Button Event
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                //Layout Setup
                setLayout(new BorderLayout());

                //Setup Menu Bar
                JMenuBar bar = new JMenuBar();
                setJMenuBar(bar);

                //Setup the main menu
                JMenu mainMenu = new JMenu("Menu");
                JMenuItem disconnect = new JMenuItem("Disconnect");
                JMenuItem exit = new JMenuItem("Exit");
                disconnect.addActionListener(this);
				exit.addActionListener(this);
				mainMenu.add(disconnect);
				mainMenu.add(exit);
                bar.add(mainMenu);

                //Setup the server action menu
                JMenu serverMenu = new JMenu("Server");
                JMenuItem uptime = new JMenuItem("Uptime");
                JMenuItem svrVer = new JMenuItem("Version");
                JMenuItem lsReconn = new JMenuItem("LS Reconnect");
                uptime.addActionListener(this);
                svrVer.addActionListener(this);
                lsReconn.addActionListener(this);
                serverMenu.add(uptime);
                serverMenu.add(svrVer);
                serverMenu.add(lsReconn);
                bar.add(serverMenu);
                
                //Setup the world action menu                
                JMenu worldMenu = new JMenu("World");
                JMenuItem lock = new JMenuItem("Lock World");
                JMenuItem unlock = new JMenuItem("Unlock World");
                JMenuItem worldSD = new JMenuItem("Shutdown World");
                lock.addActionListener(this);
                unlock.addActionListener(this);
                worldSD.addActionListener(this);
                worldMenu.add(lock);
                worldMenu.add(unlock);
                worldMenu.add(worldSD);
                bar.add(worldMenu);
                
                //Setup the zone action menu
                JMenu zoneMenu = new JMenu("Zone");
                JMenuItem zoneStatus = new JMenuItem("Zone Status");
                JMenuItem zoneBootUp = new JMenuItem("Boot Zone");
                JMenuItem zoneShutDown = new JMenuItem( "Stop Zone");
                JMenuItem zoneLock = new JMenuItem("Lock Zone");
                JMenuItem zoneUnlock = new JMenuItem("Unlock Zone");
                JMenuItem viewLocked = new JMenuItem("View Locked Zones");
                zoneStatus.addActionListener(this);
                zoneBootUp.addActionListener(this);
                zoneShutDown.addActionListener(this);
                zoneLock.addActionListener(this);
                zoneUnlock.addActionListener(this);
                viewLocked.addActionListener(this);
                zoneMenu.add(zoneStatus);
                zoneMenu.add(zoneBootUp);
                zoneMenu.add(zoneShutDown);
                zoneMenu.add(zoneLock);
                zoneMenu.add(zoneUnlock);
                zoneMenu.add(viewLocked);
                bar.add(zoneMenu);               

                //Setup the account menu
                JMenu accountMenu = new JMenu("Accounts");
                JMenuItem add = new JMenuItem("Add Account");
                JMenuItem delete = new JMenuItem("Delete Account");
                JMenuItem status = new JMenuItem("Change Status");
                JMenuItem chgPass = new JMenuItem("Change Password");
                JMenuItem listAcct = new JMenuItem("List Accounts");
                add.addActionListener(this);
                delete.addActionListener(this);
                status.addActionListener(this);
                chgPass.addActionListener(this);
                listAcct.addActionListener(this);
                accountMenu.add(add);
                accountMenu.add(delete);
                accountMenu.add(status);
                accountMenu.add(chgPass);
                accountMenu.add(listAcct);
                bar.add(accountMenu);

                //Setup the Character menu
                JMenu charMenu = new JMenu("Characters");
                JMenuItem who = new JMenuItem("Who");
                JMenuItem kick = new JMenuItem("Kick");
                JMenuItem ipLookup = new JMenuItem("IP Lookup");
                JMenuItem move = new JMenuItem("Move Character");
                JMenuItem listChar = new JMenuItem("List Characters");
                JMenuItem charInv = new JMenuItem("Character Inventory");
                JMenuItem charBots = new JMenuItem("List Bots");
                who.addActionListener(this);
                kick.addActionListener(this);
                ipLookup.addActionListener(this);
                move.addActionListener(this);
                listChar.addActionListener(this);
                charInv.addActionListener(this);
                charBots.addActionListener(this);
                charMenu.add(who);
                charMenu.add(kick);
                charMenu.add(ipLookup);
                charMenu.add(move);
                charMenu.add(listChar);
                charMenu.add(charInv);
                charMenu.add(charBots);
                bar.add(charMenu);

                //Setup the Guild menu
                JMenu guildMenu = new JMenu("Guilds");
                JMenuItem addGuild = new JMenuItem("Create Guild");
                JMenuItem deleteGuild = new JMenuItem("Delete Guild");
                JMenuItem showGuilds = new JMenuItem("List Guilds");
                addGuild.addActionListener(this);
                deleteGuild.addActionListener(this);
                showGuilds.addActionListener(this);
                guildMenu.add(addGuild);
                guildMenu.add(deleteGuild);
                guildMenu.add(showGuilds);
                bar.add(guildMenu);
                
                //Setup the Petition menu
                JMenu petitionMenu = new JMenu("Petitions");
                JMenuItem viewPetitions = new JMenuItem("View Petitions");
                viewPetitions.addActionListener(this);
                petitionMenu.add(viewPetitions);
                bar.add(petitionMenu);
                
                //Setup the message menu
                JMenu messageMenu = new JMenu("Messaging");
                JMenuItem emote = new JMenuItem("Emote");
                JMenuItem gmsay = new JMenuItem("GM Say");
                JMenuItem ooc = new JMenuItem("OOC");
                JMenuItem tell = new JMenuItem("Tell");
                JMenuItem broadcast = new JMenuItem("Broadcast");
                JMenuItem auction = new JMenuItem("Auction");
                emote.addActionListener(this);
                gmsay.addActionListener(this);
                ooc.addActionListener(this);
                tell.addActionListener(this);
                broadcast.addActionListener(this);
                auction.addActionListener(this);
                messageMenu.add(emote);
                messageMenu.add(gmsay);
                messageMenu.add(ooc);
                messageMenu.add(tell);
                messageMenu.add(broadcast);
                messageMenu.add(auction);
                bar.add(messageMenu);
                
                //Setup the help menu
                JMenu helpMenu = new JMenu("Help");
                JMenuItem helpContents = new JMenuItem("Help Contents");
                JMenuItem about = new JMenuItem("About EQEmuJSM");
                helpContents.addActionListener(this);
                about.addActionListener(this);
                helpMenu.add(helpContents);
                helpMenu.add(about);
                bar.add(helpMenu);
                
                //Setup North Panel
                JPanel northPanel = new JPanel();
                add(northPanel, BorderLayout.NORTH);
                northPanel.setLayout(new GridLayout(1,2));
                Border headerBdr = BorderFactory.createTitledBorder("Status:");
                northPanel.setBorder(headerBdr);
                northPanel.add(telnetStatusLbl);           
                northPanel.add(dbStatusLbl);
                
                //Setup West Panel
                JPanel westPanel = new JPanel();
                add(westPanel, BorderLayout.WEST);
                westPanel.setLayout(new GridLayout(6,2));
                Border loginBdr = BorderFactory.createTitledBorder("Login Info:");
                westPanel.setBorder(loginBdr);
                JLabel svrIpLbl = new JLabel("Server IP:", JLabel.LEFT);
                JLabel svrPortLbl = new JLabel("Port:", JLabel.LEFT);
                JLabel dbNameLbl = new JLabel("DB Name:", JLabel.LEFT);
                JLabel userNameLbl = new JLabel("Username:", JLabel.LEFT);
                JLabel passLbl = new JLabel("Password:", JLabel.LEFT);
                JButton connectBtn = new JButton("Connect");
                JButton disconnectBtn = new JButton("Disconnect");
                connectBtn.addActionListener(this);
                disconnectBtn.addActionListener(this);
                westPanel.add(svrIpLbl);
                westPanel.add(svrIp);
                westPanel.add(svrPortLbl);
                westPanel.add(svrPort);
                westPanel.add(dbNameLbl);
                westPanel.add(dbName);
                westPanel.add(userNameLbl);
                westPanel.add(userName);
                westPanel.add(passLbl);
                westPanel.add(pass);
                westPanel.add(connectBtn);
                westPanel.add(disconnectBtn);                
                
                //NEEDS WORK!!!
                //Setup console output in TextArea
                //Setup Center Panel
                JPanel centerPanel = new JPanel();
                add(centerPanel, BorderLayout.CENTER);
                Border outputBdr = BorderFactory.createTitledBorder("Output:");
                centerPanel.setBorder(outputBdr);
                
                //Need to get console in output area in main window
                
                //Using Console Class - Hack
                new Console();
                
                //Using Output Class - Clean (Not Working)
                //try {
                  //  Output o = new Output();
                    //centerPanel.add(o);
                //} catch (IOException e) {
                	//e.printStackTrace();
                //}
                
                //Setup South Panel
                JPanel southPanel = new JPanel();
                southPanel.setLayout(new GridLayout());
                add(southPanel, BorderLayout.SOUTH);
                Border footerBdr = BorderFactory.createTitledBorder("Version:");
                southPanel.setBorder(footerBdr);
                JLabel versionLbl = new JLabel(version, JLabel.RIGHT);
                southPanel.add(versionLbl);
       }

        //Action Listener
        public void actionPerformed(ActionEvent e)
        {
        	String actionCommand = e.getActionCommand();

        	//Handle Exit
        	if (actionCommand.equals("Exit"))
        	{
        		try
        		{		
        			//Exit Program
            		System.exit(0);
        		}
        		catch( Exception e2 )
        		{
        			e2.printStackTrace();
        		}
        	}
        	
        	else if (actionCommand.equals("Connect"))
        	{
        		//Connect to telnet & mysql servers
        		try 
    			{
        			server = svrIp.getText();
        			port = Integer.parseInt(svrPort.getText());
        			db = dbName.getText();
        			user = userName.getText();
        			pw = String.valueOf(pass.getPassword());
        			jdbcURL = "jdbc:mysql://" + server + "/" + db;
        			
        			//Telnet Connection
        			telnet = new eqemuTelnet( server, port, user, pw );
        			telnetStatus = "Connected";
        			telnetStatusLbl.setText("Telnet Status: " + telnetStatus + " to " + server);
        			
        			//MySQL Connection
        			Class.forName("com.mysql.jdbc.Driver").newInstance();
        			mysqlConn = DriverManager.getConnection(jdbcURL, user, pw);
        			dbStatusLbl.setText("DB Connection: " + db);
        			
        			//Clear all text fields
                    svrIp.setText("");
                    svrPort.setText("");
                    dbName.setText("");
                    userName.setText("");
                    pass.setText("");
    			}
        		catch( Exception e1 ) 
    			{   
        			//Failed to connect
        			JOptionPane.showMessageDialog(frame, "Failed to connect! Please try again...");
        			
        			//DEBUG
    	 			//e1.printStackTrace();
       			}
        	}
        	
        	else if (actionCommand.equals("Disconnect"))
        	{
        		//Disconnect from the telnet & mysql servers
        		try
        		{
        			//Disconnect Telnet
        			telnet.disconnect();
        			telnetStatus = "Disconnected";
        			telnetStatusLbl.setText("Telnet Status: " + telnetStatus);
        			
        			//Disconnect MySQL
        			mysqlConn.close();
        			db = "none";
        			dbStatusLbl.setText("DB Connection: " + db);
        			
        			JOptionPane.showMessageDialog(frame, "You have been disconnected!");
        		}
        		catch( Exception e2 )
        		{
        			//Failed to disconnect
        			JOptionPane.showMessageDialog(frame, "Failed to disconnect! Please try again...");
        			
        			//DEBUG
        			//e2.printStackTrace();
        		}
        	}
        	
        	//NEEDS WORK!!!
        	else if (actionCommand.equals("Help Contents"))
    		{
        		//Show help docs
    			JOptionPane.showMessageDialog(frame, "Help docs to come ...");
    		}
    		
    		else if (actionCommand.equals("About EQEmuJSM"))
    		{
    			//Show program info
    			JOptionPane.showMessageDialog(frame, version);
    		}
 
    		else if (actionCommand.equals("Uptime"))
    		{
    			if (telnetStatus == "Connected")
    				//Server Uptime
    				telnet.sendCommand( "uptime" );
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    	    }
    		       	
    		else if (actionCommand.equals("Version"))
    		{
    			if (telnetStatus == "Connected")
    				//Server Version
    				telnet.sendCommand( "version" );
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("LS Reconnect"))
    		{
    			if (telnetStatus == "Connected")
    				//Reconnect to Login Server
    				telnet.sendCommand( "LSReconnect" );
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}

    		else if (actionCommand.equals("Lock World"))
    		{
    			if (telnetStatus == "Connected")
    				//Lock Server
    				telnet.sendCommand( "lock" );
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Unlock World"))
    		{
    			if (telnetStatus == "Connected")
    				//Unlock Server
    				telnet.sendCommand( "unlock" );  		
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Shutdown World"))
    		{
    			if (telnetStatus == "Connected")
    				//Shutdown World Server
    				telnet.sendCommand( "worldshutdown" );
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Zone Status"))
    		{
    			if (telnetStatus == "Connected")
    				//Report Zone Status
    				telnet.sendCommand( "zonestatus" );
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Lock Zone"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Lock Zone
    				zone = JOptionPane.showInputDialog(frame, "Zone: ", "");
    				telnet.sendCommand( "zonelock lock " + zone );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Boot Zone"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Boot Zone
    				zone = JOptionPane.showInputDialog(frame, "Zone: ", "");
    				telnet.sendCommand( "zonebootup " + zone );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Stop Zone"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Stop Zone
    				zone = JOptionPane.showInputDialog(frame, "Zone: ", "");
    				telnet.sendCommand( "zoneshutdown " + zone );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Unlock Zone"))
    		{
    			if (telnetStatus == "Connected")
    			{    			
    				//Unlock Zone
    				zone = JOptionPane.showInputDialog(frame, "Zone: ", "");
    				telnet.sendCommand( "zonelock unlock " + zone );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("View Locked Zones"))
    		{
    			if (telnetStatus == "Connected")
    				//View Locked Zones
    				telnet.sendCommand( "zonelock list" );
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Add Account"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				
    				//Create login server account
    				String accountName = JOptionPane.showInputDialog(frame, "Account Name: ", "");
    				
    				//Password must be in sha1 hash form
    				String accountPass = JOptionPane.showInputDialog(frame, "Account Password: ", "");
    				String accountEmail = JOptionPane.showInputDialog(frame, "Account Email: ", "");
    				
    				//Timestamp for account creation
    				java.sql.Timestamp sqlDate = new java.sql.Timestamp(new java.util.Date().getTime());
    				
    				PreparedStatement lsacctpst = null;
    				
    				try 
    				{
    					//Prepared Statement for add LS Account
    					String lsacctSQL = "INSERT INTO tblLoginServerAccounts VALUES(?,?,SHA1(?),?,?,?,?) ";
    					lsacctpst = mysqlConn.prepareStatement(lsacctSQL);
    					lsacctpst.setString(1, null);
    					lsacctpst.setString(2, accountName);
    					lsacctpst.setString(3, accountPass);
    					lsacctpst.setTimestamp(4, sqlDate);
    					lsacctpst.setString(5, accountEmail);
    					lsacctpst.setString(6, "0000-00-00 00:00:00");
    					lsacctpst.setString(7, "");
    					lsacctpst.executeUpdate();
    					lsacctpst.close();
    					System.out.println("The account " + accountName + " was successfully created!");
    				} 
    				catch (SQLException e1) 
    				{
    					// Failed
    					JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    					//DEBUG
    					//e1.printStackTrace();
    				}
    				
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Delete Account"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Delete login server account
    				String accountName = JOptionPane.showInputDialog(frame, "Account Name: ", "");
    				
    				try 
    				{
    					Statement s = mysqlConn.createStatement();
    					String lsdelSQL = "DELETE FROM tblLoginServerAccounts WHERE AccountName = '" + accountName + "'"; 
    					s.executeUpdate(lsdelSQL);
    					s.close();
    					System.out.println("The login server account " + accountName + " was successfully deleted.");
    				} 
    				catch (SQLException e1) 
    				{
    					// Failed
    					JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    					//DEBUG
    					//e1.printStackTrace();
    				}
    				
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Change Status"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Change account status
    				account = JOptionPane.showInputDialog(frame, "Account: ", "");
    				status = JOptionPane.showInputDialog(frame, "Status: ", "");
    				telnet.sendCommand( "flag " + status + account );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
        	else if (actionCommand.equals("Change Password"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Change login server account password
    				account = JOptionPane.showInputDialog(frame, "Account: ", "");
    				newpass = JOptionPane.showInputDialog(frame, "New Password: ", "");
    				
    				//Telnet command to change password for admin users access
    				//not necessary for general account management
    				//telnet.sendCommand( "setpass " + account + " " + newpass);
    				
    				PreparedStatement lspwpst = null;
    				
    				try 
    				{
    					//Prepared Statement for change LS Password
    					//Password must be in sha1 hash form
    					String lspwSQL = "UPDATE tblLoginServerAccounts SET AccountPassword = SHA1(?) WHERE AccountName = ?";
    					lspwpst = mysqlConn.prepareStatement(lspwSQL);
    					lspwpst.setString(1, newpass);
    					lspwpst.setString(2, account);
    					lspwpst.executeUpdate();
    					lspwpst.close();
    					System.out.println("The account password for " + account + " was successfully changed!");
    				} 
    				catch (SQLException e1) 
    				{
    					// Failed
    					JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    					//DEBUG
    					//e1.printStackTrace();
    				}
    				
        		}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("List Accounts"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				try 
    				{
    					//List all server accounts and account status
    					Statement s = mysqlConn.createStatement();
    					s.executeQuery("select name, charname, status from account;");
    					ResultSet rs = s.getResultSet();
    					int count = 0;
    					System.out.println("Account List:");
    					while (rs.next())
    					{
    						
    						String nameVal = rs.getString("name");
    						String lastChar = rs.getString("charname");
    						int gmStatus = rs.getInt("status");
    						String statusLvl;
    						
    						//Assign status ID to a string
    						switch(gmStatus)
    						{
    							case 0: statusLvl = "Player";	break;
    							case 10: statusLvl = "Steward";	break;
    							case 20: statusLvl = "Apprentice Guide";	break;
    							case 80: statusLvl = "Guide";	break;
    							case 100: statusLvl = "GM-Admin";	break;
    							case 150: statusLvl = "GM-Lead Admin";	break;
    							case 200: statusLvl = "GM-Mgmt";	break;
    							case 250: statusLvl = "GOD";	break;    							
    							default: statusLvl = "Invalid";	break;
    						}
    						
    						System.out.println(nameVal + " " + lastChar + " " + "**" + statusLvl + "**");
    						++count;
    					}
    					rs.close();
    					s.close();
    					System.out.println("There are " + count + " accounts in the database");
    				} 
    				catch (SQLException e1) 
    				{
    					// Failed
    					JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    					//DEBUG
    					//e1.printStackTrace();
    				}
    		
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Who"))
    		{
    			if (telnetStatus == "Connected")
    				//List characters online
    				telnet.sendCommand( "who" );
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Kick"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Kick Player
    				character = JOptionPane.showInputDialog(frame, "Character Name: ", "");
    				telnet.sendCommand( "kick " + character );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("IP Lookup"))
    		{
    			if (telnetStatus == "Connected")
    			{
	    			//Lookup Player's IP
	    			character = JOptionPane.showInputDialog(frame, "Character Name: ", "");
	    			telnet.sendCommand( "IPLookup " + character );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}	
        	
        	else if (actionCommand.equals("Move Character"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Move Character
    				//Character must be offline
    				int answer = JOptionPane.showConfirmDialog(frame, "Is the character you are about to move offline?");

    				//Character offline - proceed with move
    				if (answer == JOptionPane.YES_OPTION)
    				{
    					character = JOptionPane.showInputDialog(frame, "Character Name: ", "");
    					zone = JOptionPane.showInputDialog(frame, "Move to (Zone): ", "");
    					PreparedStatement movepst = null;
    				
    					try 
    					{
    						//Prepared Statement for character move
    						String moveSQL = "UPDATE character_ SET zonename = ? WHERE name = ?";
    						movepst = mysqlConn.prepareStatement(moveSQL);
    						movepst.setString(1, zone);
    						movepst.setString(2, character);
    						movepst.executeUpdate();
    						movepst.close();
    						System.out.println("The character " + character + " was successfully move to " + zone + "!");
    					} 
    					catch (SQLException e1) 
    					{
    						// Failed
    						JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    						//DEBUG
    						//e1.printStackTrace();
    					}
    				}
    				else
    				{
    					//Clicked No or Cancel
    					JOptionPane.showMessageDialog(frame, "Check the who list under the character menu to confirm.");
    				}
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("List Characters"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				try 
    				{
    					//List all characters in the database with their level and class
    					Statement s = mysqlConn.createStatement();
    					s.executeQuery("select id, name, level, class from character_;");
    					ResultSet rs = s.getResultSet();
    					int count = 0;
    					System.out.println("Character List:");
    					while (rs.next())
    					{
    						int charID = rs.getInt("id");
    						String nameVal = rs.getString("name");
    						int lvlVal = rs.getInt("level");
    						int classVal = rs.getInt("class");
    						String classname;
    						
    						//Assign a string to class id
    						switch(classVal)
    						{
    							case 1: classname = "Warrior";	break;
    							case 2: classname = "Cleric";	break;
    							case 3: classname = "Paladin";	break;
    							case 4: classname = "Ranger";	break;
    							case 5: classname = "Shadow Knight"; break;
    							case 6: classname = "Druid";	break;
    							case 7: classname = "Monk";		break;
    							case 8: classname = "Bard";		break;
    							case 9:	classname = "Rogue";	break;
    							case 10: classname = "Shaman";	break;
    							case 11: classname = "Necromancer";	break;
    							case 12: classname = "Wizard";	break;
    							case 13: classname = "Mage";	break;
    							case 14: classname = "Enchanger";	break;
    							case 15: classname = "Beastlord";	break;
    							case 16: classname = "Berserker";	break;
    							default: classname = "Invalid";	break;
    							
    						}
    						
    						System.out.println(charID + " " + nameVal + " " + lvlVal + " " + classname);
    						++count;
    					}
    					rs.close();
    					s.close();
    					System.out.println("There are " + count + " characters in the database");
    				} 
    				catch (SQLException e1) 
    				{
    					// Failed
    					JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    					//DEBUG
    					//e1.printStackTrace();
    				}
    		
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Character Inventory"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				try 
    				{
    					//Show a specific character's inventory
    					String charName = JOptionPane.showInputDialog(frame, "Character Name: ", "");
    					
    					Statement s = mysqlConn.createStatement();
    					s.executeQuery("select Name from items i JOIN inventory v on i.id=v.itemid where v.charid=(select id from character_ where name='" + charName + "');");
    					ResultSet rs = s.getResultSet();
    					int count = 0;
    					System.out.println(charName + "'s Inventory:");
    					while (rs.next())
    					{
    						String nameVal = rs.getString("Name");
    						    						
    						System.out.println(nameVal);
    						++count;
    					}
    					rs.close();
    					s.close();
    					System.out.println(charName + " has " + count + " items in inventory.");
    				} 
    				catch (SQLException e1) 
    				{
    					// Failed
    					JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    					//DEBUG
    					//e1.printStackTrace();
    				}
    		
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("List Bots"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				try 
    				{
    					//List all bots in the database with their owner's name, level, and class
    					Statement s = mysqlConn.createStatement();
    					s.executeQuery("select c.Name,b.Name,b.Class,b.BotLevel from bots b join character_ c on b.BotOwnerCharacterID=c.id order by c.Name;");
    					ResultSet rs = s.getResultSet();
    					int count = 0;
    					System.out.println("Characer Bot List:");
    					while (rs.next())
    					{
    						String charName = rs.getString("c.Name");
    						String botName = rs.getString("b.Name");
    						int lvlVal = rs.getInt("b.BotLevel");
    						int classVal = rs.getInt("b.Class");
    						String classname;
    						
    						//Assign a string to class id
    						switch(classVal)
    						{
    							case 1: classname = "Warrior";	break;
    							case 2: classname = "Cleric";	break;
    							case 3: classname = "Paladin";	break;
    							case 4: classname = "Ranger";	break;
    							case 5: classname = "Shadow Knight"; break;
    							case 6: classname = "Druid";	break;
    							case 7: classname = "Monk";		break;
    							case 8: classname = "Bard";		break;
    							case 9:	classname = "Rogue";	break;
    							case 10: classname = "Shaman";	break;
    							case 11: classname = "Necromancer";	break;
    							case 12: classname = "Wizard";	break;
    							case 13: classname = "Mage";	break;
    							case 14: classname = "Enchanger";	break;
    							case 15: classname = "Beastlord";	break;
    							case 16: classname = "Berserker";	break;
    							default: classname = "Invalid";	break;
    							
    						}
    						
    						System.out.println(charName + " - " + botName + " " + lvlVal + " " + classname);
    						++count;
    					}
    					rs.close();
    					s.close();
    					System.out.println("There are " + count + " bots in the database");
    				} 
    				catch (SQLException e1) 
    				{
    					// Failed
    					JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    					//DEBUG
    					//e1.printStackTrace();
    				}
    		
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
    			
    		else if (actionCommand.equals("Emote"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Send Emote
    				msg = JOptionPane.showInputDialog(frame, "Your Message: ", "");
    				emoteTarget = JOptionPane.showInputDialog(frame, "Zone Name, Character, or World: ", "");
    				emoteType = JOptionPane.showInputDialog(frame, "Type: ", "");
    				telnet.sendCommand( "emote " + emoteTarget + " " + emoteType + " " + msg );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("GM Say"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Send GM Say
    				msg = JOptionPane.showInputDialog(frame, "Your Message: ", "");
    				telnet.sendCommand( "gmsay " + msg );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("OOC"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Send OOC
    				msg = JOptionPane.showInputDialog(frame, "Your Message: ", "");
    				telnet.sendCommand( "ooc " + msg );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Tell"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Send Tell
    				character = JOptionPane.showInputDialog(frame, "Character Name: ", "");
    				msg = JOptionPane.showInputDialog(frame, "Your Message: ", "");
    				telnet.sendCommand( "tell " + character + " " + msg );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Broadcast"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Send Broadcast
    				msg = JOptionPane.showInputDialog(frame, "Your Message: ", "");
    				telnet.sendCommand( "broadcast " + msg );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Auction"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Send Auction
    				msg = JOptionPane.showInputDialog(frame, "Your Message: ", "");
    				telnet.sendCommand( "auction " + msg );
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Create Guild"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Create guild
    				String guildName = JOptionPane.showInputDialog(frame, "Guild Name: ", "");
    				String leaderID = JOptionPane.showInputDialog(frame, "Leader ID: ", "");
    				PreparedStatement guildpst = null;
    				
    				try 
    				{
    					//Prepared Statement for add guild
    					String guildSQL = "INSERT INTO guilds VALUES(?,?,?,?,?,?,?,?,?) ";
    					guildpst = mysqlConn.prepareStatement(guildSQL);
    					guildpst.setString(1, null);
    					guildpst.setString(2, guildName);
    					guildpst.setString(3, leaderID);
    					guildpst.setString(4, "0");
    					guildpst.setString(5, "");
    					guildpst.setString(6, "0");
    					guildpst.setString(7, "");
    					guildpst.setString(8, "");
    					guildpst.setString(9, "");
    					guildpst.executeUpdate();
    					guildpst.close();
    					System.out.println("The guild " + guildName + " was successfully created!");
    				} 
    				catch (SQLException e1) 
    				{
    					// Failed
    					JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    					//DEBUG
    					//e1.printStackTrace();
    				}
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("Delete Guild"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//Delete Guild
    				String guildName = JOptionPane.showInputDialog(frame, "Guild Name: ", "");
    				
    				try 
    				{
    					Statement s = mysqlConn.createStatement();
    					String gdelSQL = "DELETE FROM guilds WHERE name = '" + guildName + "'"; 
    					s.executeUpdate(gdelSQL);
    					s.close();
    					System.out.println("The guild " + guildName + " was successfully deleted.");
    				} 
    				catch (SQLException e1) 
    				{
    					// Failed
    					JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    					//DEBUG
    					//e1.printStackTrace();
    				}
    			}
    			else
    				//Connect First!	
    				JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
        	}
        	
    		else if (actionCommand.equals("List Guilds"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//View all Guilds
    				try 
    				{
    					Statement s = mysqlConn.createStatement();
    					s.executeQuery("SELECT id, name, leader FROM guilds");
    					ResultSet rs = s.getResultSet();
    					int count = 0;
    					while (rs.next())
    					{
    						int idVal = rs.getInt("id");
    						String nameVal = rs.getString("name");
    						int leaderVal = rs.getInt("leader");
    						//Match leader val with charID to get name displayed
    						System.out.println(idVal + " " + nameVal + " " + leaderVal);
    						++count;
    					}
    					rs.close();
    					s.close();
    					System.out.println("There are " + count + " guilds are in the database");
    				} 
    				catch (SQLException e1) 
    				{
    					// Failed
    					JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    					//DEBUG
    					//e1.printStackTrace();
    				}
    			}
    			else
        			//Connect First!	
        			JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        	
    		else if (actionCommand.equals("View Petitions"))
    		{
    			if (telnetStatus == "Connected")
    			{
    				//View all petitions
    				try 
    				{
    					Statement s = mysqlConn.createStatement();
    					s.executeQuery("SELECT petid, charname, petitiontext, urgency FROM petitions");
    					ResultSet rs = s.getResultSet();
    					int count = 0;
    					while (rs.next())
    					{
    						int petidVal = rs.getInt("petid");
    						String charnameVal = rs.getString("charname");
    						String petitionVal = rs.getString("petitiontext");
    						String urgencyVal = rs.getString("urgency");
    						System.out.println(petidVal + " " + charnameVal + " " + petitionVal + " " + urgencyVal);
    						++count;
    					}
    					rs.close();
    					s.close();
    					System.out.println(count + " petitions are in the queue.");
    				} 
    				catch (SQLException e1) 
    				{
    					// Failed
    					JOptionPane.showMessageDialog(frame, "Failed to issue database query! Please check your connection and try again.");
    					//DEBUG
    					//e1.printStackTrace();
    				}
    			}
    			else
        			//Connect First!	
        			JOptionPane.showMessageDialog(frame, "You must connect to a server first!");
    		}
        }
    
}
//End Of File